var request = require("../../utils/mr_xu/request.js")
var api = require("../../utils/mr_xu/api_url.js").api
var orderCache = require('../../utils/mr_xu/apply_order_cache.js')
var common = require('../../utils/mr_xu/common.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    carImageList: {
      type: Array,
      value: [],
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    carImageList: []
  },
  /**
   * 组件的方法列表
   */
  attached() {
    this.setData({
      carImageList : this.properties.carImageList
    })
  },
  methods: {
    preview(e) {
      console.log(e)
      let url = e.currentTarget.dataset.url
      wx.previewImage({
        urls: [url],
      })
    },
    deleteImage(e) {
      let index = e.currentTarget.dataset.index
      let that = this
      wx.showModal({
        title: '提示',
        content: '确定要删除吗？',
        success: function(sm) {
          if (sm.confirm) {
            let carImageList = that.data.carImageList
            carImageList.splice(index,1)
            that.setData({
              carImageList: carImageList
            })
            that.triggerEvent("imagesChange",carImageList)
          } else if (sm.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    },
    add_photo: function() {
      let that = this
      wx.chooseImage({
        count: 9,
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {

          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          if (tempFilePaths.length) {
            let images = that.data.carImageList
            for (let i = 0; i < tempFilePaths.length; i++) {
              let image = wx.getFileSystemManager().readFileSync(res.tempFilePaths[i], "base64")
              common.upload(res.tempFilePaths[i], function(realUrl) {
                let carImageList = that.data.carImageList
                carImageList.push({
                  image: realUrl,
                  src: res.tempFilePaths[i]
                })
                that.setData({
                  carImageList: carImageList
                })

                that.triggerEvent('imagesChange', that.data.carImageList)

              })
            }
          }
        },
      })
    },
  }
})